package fr.esisar.cs420.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.wb.swt.SWTResourceManager;

import fr.esisar.cs420.traitement.Utilisateur;

public class CreationCompte extends Shell {
	
	private Text mail;
	private Text mdp;
	private Text nom;
	private Text prenom;
	private Text adresse;
	private Text codePostal;
	private Text ville;
	private Text erreurMail;
	private Text erreurMdp;
	private Text erreurAdresse;
	private Text erreurCodePostal;
	private Text erreurVille;
	private Text erreurPrenom;
	private Text erreurNom;

	/**
	 * Create the shell.
	 * @param display
	 */
	public CreationCompte(Display display) {
		super(display, SWT.SHELL_TRIM);
		
		Group grpInformations = new Group(this, SWT.NONE);
		grpInformations.setText("Informations");
		grpInformations.setBounds(10, 0, 519, 323);
		
		Label lblMail = new Label(grpInformations, SWT.NONE);
		lblMail.setBounds(23, 26, 55, 15);
		lblMail.setText("Mail * :");
		
		mail = new Text(grpInformations, SWT.BORDER);
		mail.setBounds(114, 25, 260, 21);
			
		Label lblMotDePasse = new Label(grpInformations, SWT.NONE);
		lblMotDePasse.setBounds(23, 57, 86, 15);
		lblMotDePasse.setText("Mot de passe * :\r\n");
		
		mdp = new Text(grpInformations, SWT.BORDER);
		mdp.setBounds(114, 55, 260, 21);
		
		Label lblNom = new Label(grpInformations, SWT.NONE);
		lblNom.setBounds(23, 88, 55, 15);
		lblNom.setText("Nom * :");
		
		nom = new Text(grpInformations, SWT.BORDER);
		nom.setBounds(114, 85, 260, 21);
		
		Label lblPrenom = new Label(grpInformations, SWT.NONE);
		lblPrenom.setBounds(23, 123, 55, 15);
		lblPrenom.setText("Prenom * :");
		
		prenom = new Text(grpInformations, SWT.BORDER);
		prenom.setBounds(114, 120, 260, 21);
		
		Label lblAdresse = new Label(grpInformations, SWT.NONE);
		lblAdresse.setBounds(23, 159, 55, 15);
		lblAdresse.setText("Adresse * :");
		
		adresse = new Text(grpInformations, SWT.BORDER);
		adresse.setBounds(114, 153, 260, 21);
		
		Label lblCodePostal = new Label(grpInformations, SWT.NONE);
		lblCodePostal.setBounds(23, 196, 86, 15);
		lblCodePostal.setText("Code postal * :");
		
		codePostal = new Text(grpInformations, SWT.BORDER);
		codePostal.setBounds(114, 190, 260, 21);
		
		ville = new Text(grpInformations, SWT.BORDER);
		ville.setBounds(114, 229, 260, 21);
		
		Label lblVille = new Label(grpInformations, SWT.NONE);
		lblVille.setBounds(23, 232, 55, 15);
		lblVille.setText("Ville  * :");
		
		Button btnValider = new Button(grpInformations, SWT.NONE);
		btnValider.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				
				erreurMail.setText("");
				erreurMdp.setText("");
				erreurAdresse.setText("");
				erreurCodePostal.setText("");
				erreurVille.setText("");
				erreurNom.setText("");
				erreurPrenom.setText("");
				
				
				if( mail.getText() == ""){ 
					
					erreurMail.setText("Champ obligatoire");
				
				}else if (Utilisateur.existanceCompte(mail.getText())){
					
					erreurMail.setText("Mail déjà utilisé.");
					
				}else if ( mdp.getText() == ""){
					
					erreurMdp.setText("Champ obligatoire");
					
				}else if ( nom.getText() == ""){
					
					erreurNom.setText("Champ obligatoire");
					
				}else if ( prenom.getText() == ""){
					
					erreurPrenom.setText("Champ obligatoire");
					
				}	
				else if ( adresse.getText() == ""){
					
					erreurAdresse.setText("Champ obligatoire");
					
				}else if ( codePostal.getText() == ""){
					
					erreurCodePostal.setText("Champ obligatoire");
					
					
				}else if ( ville.getText() == "") {
					
					erreurVille.setText("Champ obligatoire");
				
				}else{
				
					try{
								
								int code = Integer.valueOf(codePostal.getText()) ;
								Utilisateur.creerCompte(mail.getText(), mdp.getText(), nom.getText(), prenom.getText(), adresse.getText(), code , ville.getText());						
								fermer();
					}
						catch(NumberFormatException e1 ){
							
							erreurCodePostal.setText("Incorrect");
					}
										
				}
				
				
			}
		});
		btnValider.setBounds(334, 288, 75, 25);
		btnValider.setText("Valider");
		
		Button btnAnnuler = new Button(grpInformations, SWT.NONE);
		btnAnnuler.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				
				fermer();
			}
		});
		btnAnnuler.setBounds(415, 288, 75, 25);
		btnAnnuler.setText("Annuler");
		
		Label lblChamps = new Label(grpInformations, SWT.NONE);
		lblChamps.setBounds(10, 288, 129, 15);
		lblChamps.setText("* : Champs obligatoires");
		
		erreurMail = new Text(grpInformations, SWT.NONE);
		erreurMail.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		erreurMail.setBounds(384, 25, 106, 21);
		
		erreurMdp = new Text(grpInformations, SWT.NONE);
		erreurMdp.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		erreurMdp.setBounds(384, 55, 106, 21);
		
		erreurAdresse = new Text(grpInformations, SWT.NONE);
		erreurAdresse.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		erreurAdresse.setBounds(384, 153, 106, 21);
		
		erreurCodePostal = new Text(grpInformations, SWT.NONE);
		erreurCodePostal.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		erreurCodePostal.setBounds(384, 190, 106, 21);
		
		erreurVille = new Text(grpInformations, SWT.NONE);
		erreurVille.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		erreurVille.setBounds(384, 229, 106, 21);
		
		erreurPrenom = new Text(grpInformations, SWT.NONE);
		erreurPrenom.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		erreurPrenom.setBounds(384, 120, 106, 21);
		
		erreurNom = new Text(grpInformations, SWT.NONE);
		erreurNom.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		erreurNom.setBounds(384, 85, 106, 21);
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("Création Compte");
		setSize(551, 370);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	protected void fermer(){
		
		this.dispose();
		
	}
	
	
}
